package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.jaeger.library.StatusBarUtil;
import com.makeramen.roundedimageview.RoundedImageView;
import com.project.base.baseproject.Activity.BaseActivity.BaseActivity;
import com.project.base.baseproject.AppCustoms.Tool.ToastUtil;
import com.project.base.baseproject.R;
import com.zxy.tiny.Tiny;
import com.zxy.tiny.callback.BitmapCallback;

import java.io.Serializable;
import java.util.ArrayList;

//图片多选 浏览图片
public class MuliteSelectBorwseActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    //当前显示的下标
    int currIndex = 0;
    //当前需要显示的图片
    ArrayList<BangBangPhoto> bangBangPhotos;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mulite_select_browse_layout);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white),0);
        //init
        bangBangPhotos = new ArrayList<>();
        //intent中得到需要显示的图片
        if(getIntent() != null)
            if(getIntent().getSerializableExtra("browseImage") != null)
                bangBangPhotos = (ArrayList<BangBangPhoto>) getIntent().getSerializableExtra("browseImage");
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return bangBangPhotos.size();
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                final PhotoView photoView = new PhotoView(MuliteSelectBorwseActivity.this);
                photoView.enable();
                photoView.setMaxScale(4);
                final BangBangPhoto bangBangPhoto = bangBangPhotos.get(position);
                if(bangBangPhoto.getOiginalImage() != null) {
                    photoView.setImageBitmap(bangBangPhoto.getOiginalImage());
                } else {
                    Tiny.BitmapCompressOptions options = new Tiny.BitmapCompressOptions();
                    //options.height = xxx;//some compression configuration.
                    Tiny.getInstance().source(bangBangPhoto.getLocalPath()).asBitmap().withOptions(options).compress(new BitmapCallback() {
                        @Override
                        public void callback(boolean isSuccess, Bitmap bitmap) {
                            //return the compressed bitmap object
                            photoView.setImageBitmap(bitmap);
                            bangBangPhoto.setOiginalImage(bitmap);
                        }
                    });
                }
                container.addView(photoView);
                return photoView;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        viewPager.addOnPageChangeListener(this);
        setCurrNumberInfo();
        findViewById(R.id.return_btn).setOnClickListener(this);
        findViewById(R.id.finish_btn).setOnClickListener(this);
        findViewById(R.id.moreselect).setOnClickListener(this);
    }
    //设置当前数量
    private void setCurrNumberInfo() {
        RoundedImageView messageNumberBg = (RoundedImageView) findViewById(R.id.messageNumberBg);
        TextView messageNumber = (TextView) findViewById(R.id.messageNumber);
        if(getSelectedNumber() == 0) {
            messageNumberBg.setVisibility(View.GONE);
            messageNumber.setVisibility(View.GONE);
        } else {
            messageNumberBg.setVisibility(View.VISIBLE);
            messageNumber.setVisibility(View.VISIBLE);
            messageNumber.setText(getSelectedNumber()+"");
        }
    }
    //设置当前是否被选中
    private void setSelectInfo() {
        ImageView imageView = (ImageView) findViewById(R.id.moreselect);
        BangBangPhoto bangBangPhoto = bangBangPhotos.get(currIndex);
        if(bangBangPhoto.isSelected())
            imageView.setImageResource(R.drawable.pictures_selected);
        else
            imageView.setImageResource(R.drawable.picture_unselected);
    }
    //获得当前一共选择了多少张
    private int getSelectedNumber() {
        int count = 0;
        for (BangBangPhoto bang : bangBangPhotos) {
            if(bang.isSelected())
                count++;
        }
        return count;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.finish_btn:
                if(getSelectedNumber() == 0) {
                    ToastUtil.show(this,"你还没有选择任何图片");
                    return;
                }
                for (BangBangPhoto bang : bangBangPhotos) {
                    bang.setOiginalImage(null);
                }
                Intent intent1 = new Intent();
                intent1.putExtra("selectImage",(Serializable) bangBangPhotos);
                setResult(RESULT_OK,intent1);
                finish();
                break;
            case R.id.moreselect:
                BangBangPhoto bangBangPhoto = bangBangPhotos.get(currIndex);
                bangBangPhoto.setSelected(!bangBangPhoto.isSelected());
                setCurrNumberInfo();
                setSelectInfo();
                break;
            case R.id.return_btn:
                Intent intent = new Intent();
                for (BangBangPhoto bang : bangBangPhotos) {
                    bang.setOiginalImage(null);
                }
                intent.putExtra("selectImage",(Serializable) bangBangPhotos);
                setResult(RESULT_CANCELED,intent);
                finish();
                break;
            default:
                break;
        }
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
    @Override
    public void onPageSelected(int position) {
        currIndex = position;
        setSelectInfo();
    }
    @Override
    public void onPageScrollStateChanged(int state) {}
}
